# Input:    /Data/finalData.RData
# Output:   /Paper/Tables/main.tex
#           /Paper/Tables/main_SI.tex
#           /Paper/Figures/sensitivity_dyad.pdf
# Author:   JB


rm(list = ls())
require(lme4)
require(lfe)
require(tidyverse)
require(ggridges)
require(fixest)

# source('./helper_functions.R')
load('../data/finalData.RData')



# Rigorous fixed effects
dyadToAnal <- utterance_level %>%
  mutate_at(vars(matches('lag')),function(x) ifelse(is.na(x),0,x)) %>%
  mutate(votepct_rel = ifelse(is.infinite(votepct_rel),1,votepct_rel),
         respondingTo = relevel(factor(respondingTo),ref = 'FEDBERNANKE'))

dims <- colnames(utterance_level %>% select(matches('SENT_'),-matches('_lag|error')) %>% select(-matches('SEVERE|AUTHOR|LIKELY')))

# diff-in-diff
summary(mod <- feols(as.formula(paste0('interruptor ~ fedResp*yellenTime + interrupted + poly(year,3) + log(tot_utterances) + log(nchars_lag) + ',
                                      paste(paste0('topic_',1:100,'_lag'),collapse = ' + '),
                                      ' + ',
                                      paste(paste0('scale(',dims[-which(grepl('comb',dims))],'_lag)'),collapse = ' + '),
                                      '| opensecretsID + chamber')),
                    utterance_level %>%
                      mutate(yellenTime = ifelse(yellenTime,1,0)) %>%
                      filter(ind > mind),
                    cluster = c('opensecretsID')))

require(marginaleffects)
summary(mod)
toplot <- plot_cme(mod,variables = 'fedResp',condition = 'yellenTime',draw = F)

mod$coeftable %>%
  data.frame() %>%
  mutate(vars = row.names(.)) %>%
  filter(grepl(':',vars)) %>%
  select(est = Estimate,se = Std...Error,)

toadd <- toplot %>%
  select(estimate,std.error) %>%
  summarise(diff = diff(estimate),
            se = sqrt(sum(std.error^2)))

toplot <- toplot %>%
  bind_cols(toadd)

pdf('./output/figures/did_mfx.pdf',width = 7,height = 5)
pFull <- toplot %>%
  ggplot(aes(x = factor(yellenTime),y = estimate,group = term)) + 
  geom_point() + 
  geom_line() + 
  geom_errorbar(aes(ymin = conf.low,ymax = conf.high),width = 0) + 
  theme_ridges() + 
  scale_x_discrete(labels = c('Male Fed Chairs','Yellen')) + 
  geom_hline(yintercept = 0,linetype = 'dashed') + 
  geom_label(data = toplot %>%
               filter(yellenTime == 1),x = 1.5,
             aes(y = estimate - diff/2,label = paste0(round(diff,3),' (',round(se,3),')')),
             hjust = .5) + 
  labs(x = NULL,y = 'Increase in Interruptions\nAssociated with Fed Chair')
dev.off()


# Moderator by Party
summary(modP <- feols(as.formula(paste0('interruptor ~ fedResp*yellenTime*party + interrupted + poly(year,3) + log(tot_utterances) + log(nchars_lag) + ',
                                       paste(paste0('topic_',1:100,'_lag'),collapse = ' + '),
                                       ' + ',
                                       paste(paste0('scale(',dims[-which(grepl('comb',dims))],'_lag)'),collapse = ' + '),
                                       '| opensecretsID + chamber')),
                     utterance_level %>%
                       mutate(yellenTime = ifelse(yellenTime,1,0)) %>%
                       filter(ind > mind,
                              party %in% c('D','R')),
                     cluster = c('opensecretsID')))

toplotP <- plot_cme(modP,variables = 'fedResp',condition = (c('yellenTime','party')),draw = F)

toplotP <- toplotP %>%
  left_join(toplotP %>%
  group_by(party) %>%
  summarise(diff = diff(estimate),
            se = sqrt(sum(std.error^2))) %>%
    ungroup() %>%
    mutate(pval = 2*pt(diff/se,df = 1000,lower.tail = F),
           stars = ifelse(pval < .001,'***',ifelse(pval < .01,'**',
                                                   ifelse(pval < .05,'*','')))))



require(ggrepel)
pParty <- toplotP %>%
  ggplot(aes(x = factor(yellenTime),y = estimate,group = party,color = party,shape = party)) + 
  geom_point(position = position_dodge(width = 0)) + 
  geom_errorbar(aes(ymin = conf.low,ymax = conf.high),width = 0,position = position_dodge(width = 0)) + 
  geom_line(position = position_dodge(width = 0)) + 
  theme_ridges() + 
  scale_x_discrete(labels = c('Male Chairs','Yellen'),expand = c(.1,.1,.1,.7)) + 
  geom_hline(yintercept = 0,linetype = 'dashed') + 
  geom_label(data = toplotP %>%
               filter(yellenTime == 1),#direction = 'y',min.segment.length = 100,
             aes(y = estimate,label = paste0(party,': ',round(diff,3),'\n     (',round(se,2),')')),
             hjust = -.1) + 
  # annotate(geom = 'label',aes(label = paste0(round(est,3),'\n(',round(se,3),')'))) + 
  scale_color_manual(guide = 'none',name = 'Party',values = c('blue','red')) + 
  scale_shape_discrete(guide = 'none',name = 'Party') + 
  labs(x = NULL,y = NULL,
       subtitle = 'By Party')

pParty
summary(modG <- feols(as.formula(paste0('interruptor ~ fedResp*yellenTime*gender + interrupted + poly(year,3) + log(tot_utterances) + log(nchars_lag) + ',
                                        paste(paste0('topic_',1:100,'_lag'),collapse = ' + '),
                                        ' + ',
                                        paste(paste0('scale(',dims[-which(grepl('comb',dims))],'_lag)'),collapse = ' + '),
                                        '| opensecretsID')),
                      utterance_level %>%
                        mutate(yellenTime = ifelse(yellenTime,1,0)) %>%
                        filter(ind > mind),
                      cluster = c('opensecretsID')))

modG$coeftable['fedResp:yellenTime',1] - modG$coeftable[1,1]
modG$coeftable['fedResp',1]
sum(modG$coeftable[c('fedResp','fedResp:genderM'),1])
sum(modG$coeftable[c('fedResp','fedResp:yellenTime'),1])
sum(modG$coeftable[c('fedResp','fedResp:genderM','fedResp:yellenTime','fedResp:yellenTime:genderM'),1])


sum(modG$coeftable[c(1,2,3),1])
(modG$coeftable[1,1] + modG$coeftable['fedResp:genderM',1] + modG$coeftable['yellenTime:genderM',1] + 
    modG$coeftable['fedResp:yellenTime:genderM',1] + modG$coeftable['fedResp:yellenTime',1]) - 
    (modG$coeftable[1,1] + modG$coeftable['fedResp:genderM',1])

toplotG <- plot_cme(modG,variables = 'fedResp',condition = (c('yellenTime','gender')),draw = F)

toplotG <- toplotG %>%
  left_join(toplotG %>%
              group_by(gender) %>%
              summarise(diff = diff(estimate),
                        se = sqrt(sum(std.error^2))) %>%
              ungroup() %>%
              mutate(pval = 2*pt(diff/se,df = 1000,lower.tail = F),
                     stars = ifelse(pval < .001,'***',ifelse(pval < .01,'**',
                                                          ifelse(pval < .05,'*','')))))

pGender <- toplotG %>%
  ggplot(aes(x = factor(yellenTime),y = estimate,group = gender,color = gender,shape = gender)) + 
  geom_point(position = position_dodge(width = 0)) + 
  geom_errorbar(aes(ymin = conf.low,ymax = conf.high),width = 0,position = position_dodge(width = 0)) + 
  geom_line(position = position_dodge(width = 0)) + 
  theme_ridges() + 
  scale_x_discrete(labels = c('Male Chairs','Yellen'),expand = c(.1,.1,.1,.7)) + 
  geom_hline(yintercept = 0,linetype = 'dashed') + 
  geom_label(data = toplotG %>%
               filter(yellenTime == 1),#direction = 'y',min.segment.length = 100,
             aes(y = estimate,label = paste0(gender,': ',round(diff,3),'\n     (',round(se,2),')')),
             hjust = -.1) + 
  scale_color_manual(guide = 'none',name = 'Party',values = c('darkgreen','tomato')) + 
  scale_shape_discrete(guide = 'none',name = 'Party') + 
  labs(x = NULL,y = NULL,
       subtitle = 'By Gender')
pGender
require(patchwork)
pdf('../output/figures/did_mfx.pdf',width = 7,height = 5)
(pFull & labs(subtitle = 'Overall DiD')) + ((pParty & theme(axis.text.x = element_blank())) / pGender)
dev.off()


stargazer(mod,
          keep = 'fedResp|yellen|year|nchars|utterances|SENT_',
          add.lines = list(c('100 LDA Topic Loadings','Y'),
                           c('Tone Probabilities','Y'),
                           c('Chamber FE','Y'),
                           c('Speaker FE','Y')),
          star.char = c('\\dag','*','**','***'),star.cutoffs = c(.1,.05,.01,.001),
          keep.stat = c('n','rsq'),out = '../Paper/Tables/fig_7_table.tex')